package com.shop.loan.proxy;

import com.shop.loan.mapper.UserByMybatisMapper;
import com.shop.loan.model.PayOrderThird;
import com.shop.loan.model.entity.UserByMybatis;
import com.shop.loan.util.LoanOrderUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import xyz.erupt.annotation.fun.DataProxy;
import xyz.erupt.annotation.query.Condition;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Service
public class PayOrderThirdProxy implements DataProxy<PayOrderThird> {

    @Resource
    UserByMybatisMapper userByMybatisMapper;

    @Resource
    LoanOrderUtil loanOrderUtil;

    @Override
    public String beforeFetch(List<Condition> conditions) {
        conditions.removeIf(condition -> "userName".trim().equals(condition.getKey()) || "userPhone".trim().equals(condition.getKey()));
        List<Long> userIds = loanOrderUtil.getUserIdBySearch();
        String userIdStr = null;
        if (userIds != null && userIds.size() > 0) {
            userIdStr = org.apache.commons.lang3.StringUtils.join(userIds, ",");
        }
        String sql = null;
        String className = "PayOrderThird";
        if (StringUtils.hasLength(userIdStr)) {
            sql = className + ".userId in (" + userIdStr + ")";
        }
        return sql;
    }

    @Override
    public void afterFetch(Collection<Map<String, Object>> list) {
        if (list == null || list.size() == 0) {
            return;
        }
        for (Map<String, Object> stringObjectMap : list) {
            UserByMybatis userByMybatis = userByMybatisMapper.selectByPrimaryKey(Long.valueOf(stringObjectMap.get("userId").toString()));
            if (userByMybatis != null) {
                stringObjectMap.put("userInfo", "姓名：" + userByMybatis.getCardName() + "\n 手机号：" + userByMybatis.getMobile());
            }
        }
    }
}
